﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;

namespace PS3Packager.UserControls
{
    public partial class FtpEditor : UserControl
    {
        public FtpEditor()
        {
            InitializeComponent();
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string UriStr
        {
            get
            {
                // string url = string.Format("ftp://{0}:{1}@ftp.somewhere.com:21/fu/bar.zip", HttpUtility.UrlEncode(username), HttpUtility.UrlEncode(password)); 
                StringBuilder url = new StringBuilder();
                url.Append("ftp://");

                if (!string.IsNullOrEmpty(textBoxUsername.Text) || !string.IsNullOrEmpty(textBoxPassword.Text))
                {
                    url.Append(HttpUtility.UrlEncode(textBoxUsername.Text));
                    url.Append(":");
                    url.Append(HttpUtility.UrlEncode(textBoxPassword.Text));

                    url.Append("@");
                }

                url.Append(HttpUtility.UrlEncode(textBoxHost.Text));

                if ((numericUpDownPort.Value != 0) && (numericUpDownPort.Value != 21))
                {
                    url.Append(":");
                    url.Append(numericUpDownPort.Value.ToString());
                }

                url.Append("/");

                string folder = textBoxRemoteFolder.Text.TrimEnd().TrimStart('/', ' ');
                url.Append(folder);

                return url.ToString();
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    FtpUri = null;
                    return;
                }

                FtpUri = new Uri(value);
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Uri FtpUri
        {
            get
            {
                return new Uri(UriStr);
            }
            set
            {
                if (value == null)
                {
                    textBoxHost.Text = "";
                    numericUpDownPort.Value = 21;
                    textBoxUsername.Text = "";
                    textBoxPassword.Text = "";
                    textBoxRemoteFolder.Text = "";
                    
                    buttonDefaultRemoteFolder.PerformClick();
                    return;
                }

                textBoxHost.Text = value.Host;
                numericUpDownPort.Value = value.Port;
                textBoxUsername.Text = FTPLib.FTP.GetUserName(value);
                textBoxPassword.Text = FTPLib.FTP.GetPassword(value);
                textBoxRemoteFolder.Text = value.AbsolutePath;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Not implemented yet", "Not implemented", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void buttonLocateHost_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Not implemented yet", "Not implemented", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void buttonDefaultRemoteFolder_Click(object sender, EventArgs e)
        {
            textBoxUsername.Text = "FTPD12345";
            textBoxPassword.Text = "FTPD12345";
            textBoxRemoteFolder.Text = "/dev_hdd0/vsh/task";
        }
    }
}
